/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.M250;
import queenb.net.entitybean.N850;
import queenb.web.crm.store.M250Store;
import queenb.web.crm.store.N850Store;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class M250Controller extends QueenbController {

    private M250Store m250Store;
    private int pm250; // id
    private String mv251; // email
    private String mv252;// mo ta
    private String mv254;// login
    private String mv255;// pass
    private int pn850;
    private List<SelectItem> listComboN850;

    public M250Controller() {
        m250Store = new M250Store();

        Object pn250oObject = getRequest().getParameter("pm250");
        if (pn250oObject != null) {
            this.pm250 = Util.parseInt(pn250oObject);
            getDetailEditM250(pm250);
        }
    }

    public void updateM250() {
        try {
            System.out.println("getM250Store().updateM250(" + getPm250() + " , " + getLogin().getModule() + " , " + getLogin().getCompanyNo() + " , " + getPn850() + " , " + getMv251() + " , " + getMv252() + " , " + getMv251() + " , " + getMv254() + " , " + getMv255() + " , " + getLogin().getUserName());
            int row = getM250Store().updateM250(getPm250(), getLogin().getModule(), getLogin().getCompanyNo(), getPn850(), getMv251(), getMv252(), getMv251(), getMv254(), getMv255(), getLogin().getUserName());
            if (row > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {

                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
            sendRedirectUrlTab("setting.jsp?tab=header111&link15=" + Util.convertMD5(getRequest().getRequestURI()));
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }

    public List<M250> getListAllM250() {
        System.out.println("getListAllM250 is called !");

        try {
            System.out.println("getM250Store().getListM250(" + getLogin().getModule() + " , " + getLogin().getCompanyNo() + " , " + getPn850() + " , " + getLogin().getUserName());
            List<M250> list = getM250Store().getListM250(getLogin().getModule(), getLogin().getCompanyNo(), getPn850(), getLogin().getUserName());
            return list;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }

    }

    public void getDetailEditM250(int id) {
        try {
            List<M250> listM250Edit = getListAllM250();
            if (listM250Edit.size() > 0) {
                for (int i = 0; i < listM250Edit.size(); i++) {
                    if (listM250Edit.get(i).getPm250() == id) {
                        System.out.println("------------->>" + listM250Edit.get(i).getMv251());
                        this.mv251 = listM250Edit.get(i).getMv251();
                        this.mv255 = listM250Edit.get(i).getMv255();
                        this.pn850= listM250Edit.get(i).getFn850();
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error when getDetailEditM250. ERROR CODE: " + ex.getMessage());
        }
    }

    /**
     * @return the pm250
     */
    public int getPm250() {
        return pm250;
    }

    /**
     * @param pm250 the pm250 to set
     */
    public void setPm250(int pm250) {
        this.pm250 = pm250;
    }

    /**
     * @return the mv251
     */
    public String getMv251() {
        return mv251;
    }

    /**
     * @param mv251 the mv251 to set
     */
    public void setMv251(String mv251) {
        this.mv251 = mv251;
    }

    /**
     * @return the mv252
     */
    public String getMv252() {
        return mv252;
    }

    /**
     * @param mv252 the mv252 to set
     */
    public void setMv252(String mv252) {
        this.mv252 = mv252;
    }

    /**
     * @return the mv254
     */
    public String getMv254() {
        return mv254;
    }

    /**
     * @param mv254 the mv254 to set
     */
    public void setMv254(String mv254) {
        this.mv254 = mv254;
    }

    /**
     * @return the mv255
     */
    public String getMv255() {
        return mv255;
    }

    /**
     * @param mv255 the mv255 to set
     */
    public void setMv255(String mv255) {
        this.mv255 = mv255;
    }

    /**
     * @return the pn850
     */
    public int getPn850() {
        return pn850;
    }

    /**
     * @param pn850 the pn850 to set
     */
    public void setPn850(int pn850) {
        this.pn850 = pn850;
    }

    /**
     * @return the m250Store
     */
    public M250Store getM250Store() {
        return m250Store;
    }

    /**
     * @param m250Store the m250Store to set
     */
    public void setM250Store(M250Store m250Store) {
        this.m250Store = m250Store;
    }

    /**
     * @return the listComboN850
     */
    public List<SelectItem> getListComboN850() {

        listComboN850 = new ArrayList<SelectItem>();
        N850Store n850Store = new N850Store();
        try {
            System.out.println("n850Store.findAllN850ForCombo(" + getLogin().getModule() + " , " + getLogin().getCompanyNo() + " , " + 0 + " , " + getLogin().getUserName());
            List<N850> list = n850Store.findAllN850ForCombo(getLogin().getModule(), getLogin().getCompanyNo(), 0, getLogin().getUserName());
            if (list != null && list.size() > 0) {
                for (Iterator<N850> it = list.iterator(); it.hasNext();) {
                    N850 n850 = it.next();
                    SelectItem item = new SelectItem(n850.getVal(), n850.getLabel());
                    listComboN850.add(item);
                }
            }
        } catch (Exception e) {
            System.err.println("Error from getListComboN850: " + e.getMessage());
        }
        return listComboN850;
    }

    /**
     * @param listComboN850 the listComboN850 to set
     */
    public void setListComboN850(List<SelectItem> listComboN850) {
        this.listComboN850 = listComboN850;
    }
}
